import Vue from 'vue';
import App from './App';
import store from './store';
import uView from 'uview-ui';
import util from './common/tools/util.js';
import cart from './common/tools/cart.js';
import regular from './common/tools/regular.js';
import {getAccToken,getRefToken,setAccToken,setRefToken,removeAccToken,removeRefToken} from 'userToken.js'
import {
		encrypt,
		decrypt
	} from '@/utils/jsencrypt';


// 初始化全局JS弹窗
import './components/common/alert';
Vue.use(uView);
// 全局组件
Vue.component('zw-header', () => import('@/components/common/header.vue'));
Vue.component('zw-footer', () => import('@/components/common/footer.vue'));
Vue.component('zw-dialog', () => import('@/components/common/dialog.vue'));
Vue.component('bread-crumb', () => import('@/components/common/bread-crumb.vue'));

// 全局变量
Vue.prototype.$util = util;
Vue.prototype.$cart = cart;
Vue.prototype.$regular = regular;
Vue.prototype.resetSetItem = function (key, newVal) {
   if (key === 'sellerCartNum') {
       // 创建一个StorageEvent事件
       var newStorageEvent = document.createEvent('StorageEvent');
       const storage = {
           setItem: function (k, val) {
               sessionStorage.setItem(k, val);
               // 初始化创建的事件
               newStorageEvent.initStorageEvent('setItem', false, false, k, null, val, null, null);
               // 派发对象
               window.dispatchEvent(newStorageEvent)
           }
       }
       return storage.setItem(key, newVal);
   }

}
Vue.prototype.resetSetItemUser = function (key, newVal) {
   if (key === 'userInfoUpd') {
       // 创建一个StorageEvent事件
       var newStorageEvent = document.createEvent('StorageEvent');
       const storage = {
           setItem: function (k, val) {
               sessionStorage.setItem(k, val);
               // 初始化创建的事件
               newStorageEvent.initStorageEvent('setItemUser', false, false, k, null, val, null, null);
               // 派发对象
               window.dispatchEvent(newStorageEvent)
           }
       }
       return storage.setItem(key, newVal);
   }

}



Vue.config.productionTip = false;


Vue.prototype.pageTenantId = "774862";
// Vue.prototype.pageTenantId = "396398";
Vue.prototype.serverUrl = "https://shophive.yibaow.cn";
// Vue.prototype.serverUrl = "http://192.168.1.11:8090";
// Vue.prototype.totalUrl = "http://192.168.1.9:9080";
// Vue.prototype.webSocketUrl = "ws://192.168.1.10:80";
Vue.prototype.uploadUrl = "https://shophive.yibaow.cn/resource/oss/upload";
Vue.prototype.imgUrl = '/api/ImageManage/getImage?name='

//封装this.uni_request方法  
Vue.prototype.uni_request = function(params){
	
    params.method = params.method == undefined?"GET":params.method;
	
	let oriHeader = {}
	if(params.header){
		oriHeader = params.header
	}
	params.header =Object.assign({
		// 'id_type':'user',
		'Content-Type':'application/json;charset=utf-8',
		'authorization': 'Bearer ' + getAccToken(),
		'refresh_token': getRefToken(),
		'clientid':'383c41953b35476defd109ac92a82ad6',
		'webuuid': encrypt('396398')
	},oriHeader) 
    //在请求头中携带用户token信息
	if(!getAccToken()){
		// uni.navigateTo({
		// 	url:'/pages/user/login/index'
		// })
	}else{
		uni.request({
		    url: this.serverUrl+params.url,  
		    data:params.data,  
		    header:params.header,             
		    method:params.method,             
		    success: (res) => {  
				//code === 200
				if(res.data.code == 200 || res.data.code == 0 || res.data.code == 500){
					params.success(res)
				}
				//刷新用户token
				if(res.data.code == 403){
					removeAccToken()
					removeRefToken()
				}
				if(res.data.code == 401){
					
					uni.navigateTo({
						url:'/pages/user/login/index'
					})
				}
				
				
		    },
			error:()=>{
				console.log("request error")
			},
		    complete: (e) => {  
				if(e.data.code == 500){
					params.success(e)
				}
		    }  
		});   
	}
    
} 


App.mpType = 'app';

new Vue({
	store,
	...App
}).$mount();
